package com.replication.jdbc.statement;

import com.replication.jdbc.connection.ReplicationConnectionProxy;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ReplicationStatementProxy implements Statement {
    private final ReplicationConnectionProxy conn;
    private final StatementOpts stmtOpts = new StatementOpts();
    private Integer resultSetType;
    private Integer resultSetConcurrency;
    private Integer resultSetHoldability;
    private Statement stmt;
    private List<String> batchList = new ArrayList();

    public ReplicationStatementProxy(ReplicationConnectionProxy conn) {
        this.conn = conn;
    }

    public ReplicationStatementProxy(ReplicationConnectionProxy conn, int resultSetType, int resultSetConcurrency) {
        this.conn = conn;
        this.resultSetType = Integer.valueOf(resultSetType);
        this.resultSetConcurrency = Integer.valueOf(resultSetConcurrency);
    }

    public ReplicationStatementProxy(ReplicationConnectionProxy conn, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
        this.conn = conn;
        this.resultSetType = Integer.valueOf(resultSetType);
        this.resultSetConcurrency = Integer.valueOf(resultSetConcurrency);
        this.resultSetHoldability = Integer.valueOf(resultSetHoldability);
    }

    public void addBatch(String sql) throws SQLException {
        this.batchList.add(sql);
    }

    public void cancel() throws SQLException {
        if(this.stmt != null) {
            this.stmt.cancel();
        }
    }

    public void clearBatch() throws SQLException {
        this.batchList.clear();
    }

    public void clearWarnings() throws SQLException {
        if(this.stmt != null) {
            this.stmt.clearWarnings();
        }
    }

    public void close() throws SQLException {
        if(this.stmt != null) {
            this.stmt.close();
        }
    }

    public boolean execute(String sql) throws SQLException {
        this.stmt = Statements.createStatement(this.conn, sql, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.execute(sql);
    }

    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        this.stmt = Statements.createStatement(this.conn, sql, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.execute(sql, autoGeneratedKeys);
    }

    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        this.stmt = Statements.createStatement(this.conn, sql, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.execute(sql, columnIndexes);
    }

    public boolean execute(String sql, String[] columnNames) throws SQLException {
        this.stmt = Statements.createStatement(this.conn, sql, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.execute(sql, columnNames);
    }

    public int[] executeBatch() throws SQLException {
        this.stmt = Statements.createStatementOnMaster(this.conn, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        Iterator var2 = this.batchList.iterator();

        while(var2.hasNext()) {
            String sql = (String)var2.next();
            this.stmt.addBatch(sql);
        }

        return this.stmt.executeBatch();
    }

    public ResultSet executeQuery(String sql) throws SQLException {
        this.stmt = Statements.createStatement(this.conn, sql, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.executeQuery(sql);
    }

    public int executeUpdate(String sql) throws SQLException {
        this.stmt = Statements.createStatementOnMaster(this.conn, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.executeUpdate(sql);
    }

    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        this.stmt = Statements.createStatementOnMaster(this.conn, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.executeUpdate(sql, autoGeneratedKeys);
    }

    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        this.stmt = Statements.createStatementOnMaster(this.conn, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.executeUpdate(sql, columnIndexes);
    }

    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        this.stmt = Statements.createStatementOnMaster(this.conn, this.stmtOpts, this.resultSetType, this.resultSetConcurrency, this.resultSetHoldability);
        return this.stmt.executeUpdate(sql, columnNames);
    }

    public Connection getConnection() throws SQLException {
        return this.conn;
    }

    public int getFetchDirection() throws SQLException {
        return this.stmt.getFetchDirection();
    }

    public int getFetchSize() throws SQLException {
        return this.stmt.getFetchSize();
    }

    public ResultSet getGeneratedKeys() throws SQLException {
        return this.stmt.getGeneratedKeys();
    }

    public int getMaxFieldSize() throws SQLException {
        return this.stmt.getMaxFieldSize();
    }

    public int getMaxRows() throws SQLException {
        return this.stmt.getMaxRows();
    }

    public boolean getMoreResults() throws SQLException {
        return this.stmt.getMoreResults();
    }

    public boolean getMoreResults(int current) throws SQLException {
        return this.stmt.getMoreResults(current);
    }

    public int getQueryTimeout() throws SQLException {
        return this.stmt.getQueryTimeout();
    }

    public ResultSet getResultSet() throws SQLException {
        return this.stmt.getResultSet();
    }

    public int getResultSetConcurrency() throws SQLException {
        return this.stmt.getResultSetConcurrency();
    }

    public int getResultSetHoldability() throws SQLException {
        return this.stmt.getResultSetHoldability();
    }

    public int getResultSetType() throws SQLException {
        return this.stmt.getResultSetType();
    }

    public int getUpdateCount() throws SQLException {
        return this.stmt.getUpdateCount();
    }

    public SQLWarning getWarnings() throws SQLException {
        return this.stmt.getWarnings();
    }

    public boolean isClosed() throws SQLException {
        return this.stmt.isClosed();
    }

    public boolean isPoolable() throws SQLException {
        return this.stmt.isPoolable();
    }

    public void closeOnCompletion() throws SQLException {

    }

    public boolean isCloseOnCompletion() throws SQLException {
        return false;
    }

    public void setCursorName(String name) throws SQLException {
        this.stmtOpts.setCursorName(name);
    }

    public void setEscapeProcessing(boolean enable) throws SQLException {
        this.stmtOpts.setEscapeProcessing(enable);
    }

    public void setFetchDirection(int direction) throws SQLException {
        this.stmtOpts.setFetchDirection(direction);
    }

    public void setFetchSize(int rows) throws SQLException {
        this.stmtOpts.setFetchSize(rows);
    }

    public void setMaxFieldSize(int max) throws SQLException {
        this.stmtOpts.setMaxFieldSize(max);
    }

    public void setMaxRows(int max) throws SQLException {
        this.stmtOpts.setMaxRows(max);
    }

    public void setPoolable(boolean poolable) throws SQLException {
        this.stmtOpts.setPoolable(poolable);
    }

    public void setQueryTimeout(int seconds) throws SQLException {
        this.stmtOpts.setQueryTimeout(seconds);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return this.stmt.isWrapperFor(iface);
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return this.stmt.unwrap(iface);
    }
}
